<?php
/**
 * Created by PhpStorm.
 * User: ciwei
 * Date: 2017/12/4
 * Time: 15:40
 */
namespace app\index\model;

use \think\Model;

class Zan extends Model
{
    public function check($data)
    {
        $data['suid'] = session('uid');
        $res = db('zan')
            ->where([
                'uid'=>session('uid'),
                'aid'=>$data['aid']
            ])
            ->delete();
        if ($res) {
            db('answer')->where('aid',$data['aid'])->setDec('zan_num');
            unset($data['aid']);
            $data['status'] = 2;
            db('msg')->where($data)->delete();
            exit(json_encode(['error' => 0, 'info' => '取消点赞']));
        } else {
            $rs = db('zan')
                ->insert([
                    'uid'=>session('uid'),
                    'aid'=>$data['aid'],
                    'ctime'=>time(),
                ]);
            if($rs){
                db('answer')->where('aid',$data['aid'])->setInc('zan_num');
                unset($data['aid']);
                $data['status'] = 2;
                $data['ctime'] = time();
                db('msg')->insert($data);
                exit(json_encode(['error' => 0, 'info' => '点赞']));
            }else{
                exit(json_encode(['error' => 1, 'info' => '点赞失败']));
            }

        }
    }
}